package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.MessageConfiguration;
import com.liuxinlong.modules.system.controller.request.QueryMessageReq;
import com.liuxinlong.modules.system.controller.request.UpdateMessageRecordReq;
import com.liuxinlong.modules.system.controller.request.UpdateMessageReq;
import com.liuxinlong.modules.system.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息推送管理控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/message")
public class MessageController {

	@Autowired
	private MessageService messageService;

	/**
	 * 查询消息推送配置分页列表
	 *
	 * @param req 查询请求信息
	 * @return 消息推送配置分页列表
	 */
	@PostMapping("page")
	public BaseResp pageMessage(@RequestBody QueryMessageReq req) {
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("startNum",req.getStartNum());
		queryParam.put("pageSize",req.getPageSize());
		queryParam.put("businessType",req.getBusinessType());
		queryParam.put("pushStatus",req.getPushStatus());
		queryParam.put("status",req.getStatus());
		queryParam.put("operateUser",req.getOperateUser());
		List<Map<String,Object>> dataList = messageService.pageMessage(queryParam);
		int totalNum = messageService.getMessageCount(queryParam);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("查询消息推送配置分页列表成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 新增消息推送配置
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增消息推送配置",business = LogBusinessConstants.MESSAGE_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addMessage(@RequestBody UpdateMessageReq req) {
		MessageConfiguration mc = new MessageConfiguration();
		mc.setTitle(req.getTitle());
		mc.setBusinessType(req.getBusinessType());
		mc.setBusinessId(req.getBusinessId());
		mc.setPushType(req.getPushType());
		mc.setPushTime(req.getPushTime());
		mc.setTarget(req.getTarget());
		mc.setTerminal(req.getTerminal());
		mc.setContentType(req.getContentType());
		mc.setContent(req.getContent());
		mc.setMediaFile(req.getMediaFile());
		mc.setShareUser(req.getShareUser());
//		mc.setRemark(req.getRemark());
		messageService.addMessage(mc,req.getTargetDepartment(),req.getTargetRole(),req.getTargetUser());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增消息推送配置成功");
		return resp;
	}

	/**
	 * 修改消息推送配置
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改消息推送配置",business = LogBusinessConstants.MESSAGE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateMessage(@RequestBody UpdateMessageReq req) {
		MessageConfiguration mc = new MessageConfiguration();
		mc.setId(req.getId());
		mc.setTitle(req.getTitle());
		mc.setBusinessType(req.getBusinessType());
		mc.setBusinessId(req.getBusinessId());
		mc.setPushType(req.getPushType());
		mc.setPushTime(req.getPushTime());
		mc.setTarget(req.getTarget());
		mc.setTerminal(req.getTerminal());
		mc.setContentType(req.getContentType());
		mc.setContent(req.getContent());
		mc.setMediaFile(req.getMediaFile());
		mc.setShareUser(req.getShareUser());
//		mc.setRemark(req.getRemark());
		messageService.updateMessage(mc,req.getTargetDepartment(),req.getTargetRole(),req.getTargetUser());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改消息推送配置成功");
		return resp;
	}

	/**
	 * 删除消息推送配置
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除消息推送配置",business = LogBusinessConstants.MESSAGE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp deleteMessage(@RequestBody BaseDeleteReq req) {
		messageService.deleteMessage(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除消息推送配置成功");
		return resp;
	}

	/**
	 * 修改消息推送配置状态
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update-status")
	@LogAnnotation(value ="修改消息推送配置状态",business = LogBusinessConstants.MESSAGE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateMessageStatus(@RequestBody BaseDeleteReq req) {
		messageService.updateMessageStatus(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改消息推送配置状态成功");
		return resp;
	}

	/**
	 * 新增消息推送配置
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("auto-add")
	@LogAnnotation(value ="新增消息推送配置",business = LogBusinessConstants.MESSAGE_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addMessageAuto(@RequestBody UpdateMessageReq req) {
		messageService.addMessageAuto(req.getBusinessId(),req.getBusinessType());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增消息推送配置成功");
		return resp;
	}

	/**
	 * 查询消息推送目标受众配置
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("target-query")
	public BaseResp queryTargetMap(@RequestBody BaseDeleteReq req) {
		Map<String,Object> dataMap = messageService.queryTargetMap(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("查询消息推送目标受众配置成功");
		resp.setDataMap(dataMap);
		return resp;
	}

	/**
	 * 查询消息推送业务信息
	 *
	 * @param req 请求信息
	 * @return 业务信息列表
	 */
	@PostMapping("business-query")
	public BaseResp queryBusinessList(@RequestBody BaseDeleteReq req) {
		List<Map<String,Object>> dataList= messageService.queryBusinessList(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("查询消息推送业务信息成功");
		resp.setDataList(dataList);
		return resp;
	}

    /**
     * 撤回消息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("revoke")
    @LogAnnotation(value ="撤回消息",business = LogBusinessConstants.MESSAGE_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp revokeMessage(@RequestBody UpdateMessageRecordReq req) {
        messageService.revokeMessage(req.getId(),req.getMessageId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("撤回消息成功");
        return resp;
    }

	/**
	 * 导出消息推送信息列表
	 *
	 * @return 导出数据
	 */
	@PostMapping("export")
	@LogAnnotation(value = "导出消息推送信息列表", business = LogBusinessConstants.MESSAGE_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
	public void exportMessageList(HttpServletResponse response) {
		messageService.exportMessageList(response);
	}

	/**
	 * 消息推送记录
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("record")
	public BaseResp pageMessagePushRecord(@RequestBody QueryBaseReq req) {
		Map<String, Object> queryParam = new HashMap<>();
		queryParam.put("messageId",req.getId());
		queryParam.put("startNum",req.getStartNum());
		queryParam.put("pageSize",req.getPageSize());
		List<Map<String,Object>> dataList = messageService.pageMessagePushRecord(queryParam);
		int totalNum = messageService.getMessagePushRecordCount(queryParam);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("查询消息推送记录成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 消息推送失败记录
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("failed")
	public BaseResp pageMessagePushFailedLog(@RequestBody QueryBaseReq req) {
		Map<String, Object> queryParam = new HashMap<>();
		queryParam.put("recordId",req.getId());
		queryParam.put("startNum",req.getStartNum());
		queryParam.put("pageSize",req.getPageSize());
		List<Map<String,Object>> dataList = messageService.pageMessagePushFailedLog(queryParam);
		int totalNum = messageService.getMessagePushFailedLogCount(queryParam);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("查询消息推送失败记录成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}
}
